/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id: mole.c 96 2011-03-11 08:17:02Z mole1230 $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_mole.h"

static zend_class_entry *person_ce;

static PHP_MINIT_FUNCTION(mole);
static PHP_MSHUTDOWN_FUNCTION(mole);
static PHP_RINIT_FUNCTION(mole);
static PHP_RSHUTDOWN_FUNCTION(mole);
static PHP_MINFO_FUNCTION(mole);

static PHP_METHOD(Person, __construct);
static PHP_METHOD(Person, __destruct);
static PHP_METHOD(Person, getName);
static PHP_METHOD(Person, setName);

ZEND_BEGIN_ARG_INFO(arg_person_setname, 0)
	ZEND_ARG_INFO(0, name)
ZEND_END_ARG_INFO()

static zend_function_entry mole_functions[] = {
    PHP_ME(Person, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(Person, __destruct,  NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
    PHP_ME(Person, getName,     NULL, ZEND_ACC_PUBLIC)
    PHP_ME(Person, setName,     arg_person_setname, ZEND_ACC_PUBLIC)
	{NULL, NULL, NULL}	/* Must be the last line in mole_functions[] */
};

zend_module_entry mole_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"mole",
	mole_functions,
	PHP_MINIT(mole),
	PHP_MSHUTDOWN(mole),
	PHP_RINIT(mole),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(mole),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(mole),
#if ZEND_MODULE_API_NO >= 20010901
	PHP_MOLE_VERSION, /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_MOLE
ZEND_GET_MODULE(mole)
#endif
	
static PHP_MINIT_FUNCTION(mole)
{
    zend_class_entry person;

    INIT_CLASS_ENTRY(person, "Person", mole_functions);
    person_ce = zend_register_internal_class_ex(&person, NULL, NULL TSRMLS_CC);

    zend_declare_property_null(person_ce, ZEND_STRL("_name"), ZEND_ACC_PRIVATE TSRMLS_CC);

	return SUCCESS;
}

static PHP_MSHUTDOWN_FUNCTION(mole)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}

static PHP_RINIT_FUNCTION(mole)
{
	return SUCCESS;
}

static PHP_RSHUTDOWN_FUNCTION(mole)
{
	return SUCCESS;
}

static PHP_MINFO_FUNCTION(mole)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "mole support", "enabled");
	php_info_print_table_header(2, "author", "mole");
	php_info_print_table_header(2, "version", PHP_MOLE_VERSION);
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}

static PHP_METHOD(Person, __construct) {
    php_printf("__construct called.\n");
}

static PHP_METHOD(Person, __destruct) {
    php_printf("__destruct called.\n");
}

static PHP_METHOD(Person, getName) {
    zval *self, *name;
    self = getThis();

    name = zend_read_property(Z_OBJCE_P(self), self, ZEND_STRL("_name"), 0 TSRMLS_CC);

    RETURN_STRING(Z_STRVAL_P(name), 0);
}

static PHP_METHOD(Person, setName) {
    char *arg = NULL;
    int arg_len;
    zval *value, *self;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        WRONG_PARAM_COUNT;
    }

    self = getThis();

    MAKE_STD_ZVAL(value);
    ZVAL_STRINGL(value, arg, arg_len, 0);

    SEPARATE_ZVAL_TO_MAKE_IS_REF(&value);
    zend_update_property(Z_OBJCE_P(self), self, ZEND_STRL("_name"), value TSRMLS_CC);

    RETURN_TRUE;

}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
